package com.sram.controller.fromWX;

import com.sram.util.HttpUtil;
import com.sram.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by csmd on 2017/11/8.
 */
@Controller
@RequestMapping("/fromWX")
public class fromWXController {
    private static Logger logger = LoggerFactory.getLogger(fromWXController.class);

    @RequestMapping(value="/login")
    public void content(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId = request.getParameter("userId");
        System.out.printf("userId=====" + userId);
        String password = request.getParameter("password");
        System.out.printf("password=====" + password);
        String code = request.getParameter("code");

        //  response.getWriter().write(userId + password);
       // response.getWriter().flush();
        //return userId + password;


        // 设置向微信发送请求生成openId时的参数
        StringBuffer sb = new StringBuffer();
        sb.append("?appid="+"wx84a9466b304950e5");
        sb.append("&secret="+"7caf0ff4ba7dc82fa7de1b348ee5eb8f");
        sb.append("&js_code="+code);
        sb.append("&grant_type=authorization_code");
        // 向微信发送请求
        String create_openId_url="https://api.weixin.qq.com/sns/jscode2session";
        String openIdRes = null;
        try {
            logger.info("【调用微信登陆接口】=========登陆接口=========="+"调用微信登陆接口入参："+sb.toString()+"url:"+create_openId_url);
            openIdRes = HttpUtil.doGet(create_openId_url,sb.toString(),"utf-8", true);
            logger.info("【调用微信登陆接口】=========登陆接口=========="+"调用微信登陆接口出参："+openIdRes);
        } catch (Exception e1) {
//            json.put(CommonConsts.RETCODE, "00434101");
//            json.put(CommonConsts.RETMSG, "服务器异常，请稍后再试");
            logger.info("【调用微信登陆接口】=========登陆接口=========="+"调用微信登陆接口异常：",e1);
//            return json;
        }
        Map<String, Object> openIdMap = JsonUtil.parseJSON2Map(openIdRes);

    }
}
